package com.netty.esc.service.active.impl;

import com.alibaba.fastjson.JSONObject;
import com.netty.esc.config.websocket.WebSocketManager;
import com.netty.esc.entity.send.ProtocolACKDTO;
import com.netty.esc.service.active.UserActicveAction;
import com.netty.esc.service.isolate.CommonFriendService;
import com.netty.esc.service.isolate.impl.UserBroadcastImpl;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName: 11094
 * @Description:
 * @Author: zy
 * @Date: 2022/12/2 16:21
 * 用户行为
 **/
@Service
public class UserActicveActionImpl implements UserActicveAction {

    @Resource
    UserBroadcastImpl userBroadcast;
    @Resource
    CommonFriendService commonFriendService;

    @Override
    public void userOnline(ChannelHandlerContext ctx, JSONObject jsonObject){
        ProtocolACKDTO protocolACKDTO = new ProtocolACKDTO(jsonObject);
        //
        Long userId = jsonObject.getLong("userId");
        if (userId==null){
            protocolACKDTO.setCode(ProtocolACKDTO.ERROR);
            WebSocketManager.sendTextMessage(ctx, protocolACKDTO);
            return;
        }
        try {
            userBroadcast.userLogin(userId);
        }catch (Exception ex){
            protocolACKDTO.setCode(ProtocolACKDTO.ERROR);
            WebSocketManager.sendTextMessage(ctx, protocolACKDTO);
            return;
        }
        //给在线好友发送上线消息
        commonFriendService.assentFriendOnline(userId);
        protocolACKDTO.setCode(ProtocolACKDTO.SUCCESS);
        WebSocketManager.sendTextMessage(ctx,protocolACKDTO);
    }


    @Override
    public void userOffLine(Long userId) {
        commonFriendService.assentFriendDown(userId);
    }
}
